/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProductSpec;
import com.thinkgem.jeesite.modules.hk.shop.service.product.HkShopProductSpecService;

/**
 * 乐购商品规格Controller
 * @author 罗天文
 * @version 2018-05-15
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/product/hkShopProductSpec")
public class HkShopProductSpecController extends BaseController {

	@Autowired
	private HkShopProductSpecService hkShopProductSpecService;
	
	@ModelAttribute
	public HkShopProductSpec get(@RequestParam(required=false) String id) {
		HkShopProductSpec entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkShopProductSpecService.get(id);
		}
		if (entity == null){
			entity = new HkShopProductSpec();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:product:hkShopProductSpec:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkShopProductSpec hkShopProductSpec, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkShopProductSpec> page = hkShopProductSpecService.findPage(new Page<HkShopProductSpec>(request, response), hkShopProductSpec); 
		model.addAttribute("page", page);
		return "hk/shop/product/hkShopProductSpecList";
	}

	@RequiresPermissions("shop:product:hkShopProductSpec:view")
	@RequestMapping(value = "form")
	public String form(HkShopProductSpec hkShopProductSpec, Model model) {
		model.addAttribute("hkShopProductSpec", hkShopProductSpec);
		return "hk/shop/product/hkShopProductSpecForm";
	}

	@RequiresPermissions("shop:product:hkShopProductSpec:edit")
	@RequestMapping(value = "save")
	public String save(HkShopProductSpec hkShopProductSpec, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkShopProductSpec)){
			return form(hkShopProductSpec, model);
		}
		hkShopProductSpecService.save(hkShopProductSpec);
		addMessage(redirectAttributes, "保存乐购商品规格成功");
		return "redirect:"+Global.getAdminPath()+"/shop/product/hkShopProductSpec/?repage";
	}
	
	@RequiresPermissions("shop:product:hkShopProductSpec:edit")
	@RequestMapping(value = "delete")
	public String delete(HkShopProductSpec hkShopProductSpec, RedirectAttributes redirectAttributes) {
		hkShopProductSpecService.delete(hkShopProductSpec);
		addMessage(redirectAttributes, "删除乐购商品规格成功");
		return "redirect:"+Global.getAdminPath()+"/shop/product/hkShopProductSpec/?repage";
	}

}